﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Ys4GClient
{
    class MyHttp
    {
        public String httpGet(String url)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "GET";
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
            return sr.ReadToEnd();
        }

        public string httpPost(string url, string json)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "POST";
            byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(json);

            webRequest.ContentLength = buf.Length;
            webRequest.Timeout = 5000;
            webRequest.ContentType = "application/json";
            //webRequest.MaximumAutomaticRedirections = 1;
            webRequest.AllowAutoRedirect = true;
            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(buf, 0, buf.Length);
            newStream.Close();

            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
            String ret = reader.ReadToEnd();
            return ret;
        }

    }
}
